﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using kim.Common;
using System.Data;

namespace kim
{
    public partial class AddorEditProductClassList : System.Web.UI.Page
    {
        protected int typeid;
        private int Nid;
        protected string type;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = KIMRequest.GetString("type");
            typeid = KIMRequest.GetInt("typeid", 1);
            Nid = KIMRequest.GetInt("id", 0);
            labtitle.Text = "分类";
            
            if (!IsPostBack)
            {
                TreeBind(typeid);
                GetRequestValues();
                if (Nid > 0)
                    ddlParentId.SelectedValue = Nid.ToString();
            }
        }

        #region 绑定类别=================================
        private void TreeBind(int _channel_id)
        {
            BLL.CClass bll = new BLL.CClass();
            DataTable dt = bll.GetList(0, _channel_id);

            this.ddlParentId.Items.Clear();
            this.ddlParentId.Items.Add(new ListItem("无父级分类", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["Depth"].ToString());
                string Title = dr["ClassName"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlParentId.Items.Add(new ListItem(Title, Id));
                }
            }
        }
        #endregion
        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                {
                    lbmsg.Text = "修改成功";
                    Response.Redirect("ProductClassList.aspx?typeid=" + typeid);
                }
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                {
                    lbmsg.Text = "添加成功";
                    Response.Redirect("ProductClassList.aspx?typeid=" + typeid);
                }
                else
                    lbmsg.Text = "添加失败";
            }
           
        }
        #region 编辑添加基本操作=========================================
        private bool DoEdit(int id)
        {
            bool Edit;
            Edit= new BLL.CClass().UpdateField(id,"ClassName='"+txtTitle.Text.Trim()+"'");
            Edit = new BLL.CClass().UpdateField(id, "Orders='" + txt_postion.Text.Trim() + "'");
            Edit = new BLL.CClass().UpdateField(id, "MinPic='" + Headpic2.Src + "'");
            Edit = new BLL.CClass().UpdateField(id, "MaxPic='" + Headpic.Src + "'");

            return Edit;
        }

        private bool DoAdd()
        {
            Model.CClass mo = new Model.CClass();
            mo.ParentID = int.Parse(ddlParentId.SelectedValue);
            mo.ClassName = txtTitle.Text;
            mo.ChannelID = this.typeid;
            mo.Orders = Convert.ToInt32(txt_postion.Text);
            mo.MinPic = Headpic2.Src;
            mo.MaxPic= Headpic.Src;
            return new BLL.CClass().Add(mo);
           
        }
        #endregion
        private void GetRequestValues()
        {
           
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.CClass().Del(Nid);
                if (del)
                    Response.Redirect("ProductClassList.aspx?typeid=" + typeid);
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
          
        }
        private void GetEditValue()
        {
          
            Model.CClass model = new Model.CClass();
            BLL.CClass bll = new BLL.CClass();
            model = bll.GetModel(Nid);
            txtTitle.Text = model.ClassName;
            Headpic.Src = model.MaxPic;
            Headpic2.Src = model.MinPic;
            txt_postion.Text = model.Orders.ToString();
           
        }
        protected void htn_upload_Click(object sender, EventArgs e)//上传
        {
            string strpic = PubFileAndImageFunc.fileuploadcode(this.FileUpload1, "../../pic/");
            ViewState["img"] = strpic != "" ? strpic : this.Headpic.Src.ToString();
            this.Headpic.Src = ViewState["img"].ToString();
        }

        protected void btn_sc2_Click(object sender, EventArgs e)
        {
            string strpic = PubFileAndImageFunc.fileuploadcode(this.FileUpload2, "../../pic/");
            ViewState["img2"] = strpic != "" ? strpic : this.Headpic2.Src.ToString();
            this.Headpic2.Src = ViewState["img2"].ToString();
        }
    }
}